package com.gewu.pm.ui.dialog;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.StringRes;

import com.gewu.pm.R;
import com.gewu.pm.aop.SingleClick;
import com.hjq.base.BaseDialog;

/**
 * 通用提示弹框 两个按钮
 */
public class TipsTwoDialog {
    public static final class Builder
            extends BaseDialog.Builder<Builder> {

        private OnListener mListener;
        private final TextView tvTitle;//标题

        private final Button btCancel;//取消按钮
        private final Button btConfirm;//确定按钮

        public Builder(Context context) {
            super(context);
            setContentView(R.layout.dialog_two_tips);
            setAnimStyle(BaseDialog.ANIM_SCALE);
            setCanceledOnTouchOutside(false);//点击背景不关闭
            tvTitle = findViewById(R.id.tv_title);
            btCancel = findViewById(R.id.bt_cancel);
            btConfirm = findViewById(R.id.bt_confirm);
            setOnClickListener(btCancel, btConfirm);

        }

        /**
         * 设置标题
         */
        public Builder setTitle(@StringRes int id) {
            return setTitle(getString(id));
        }

        public Builder setTitle(CharSequence text) {
            tvTitle.setText(text);
            return this;
        }

        /**
         * 设置左边取消按钮文字
         */
        public Builder setLiftText(@StringRes int id) {
            return setLiftText(getString(id));
        }

        public Builder setLiftText(CharSequence text) {
            btCancel.setText(text);
            return this;
        }

        /**
         * 设置右边确定按钮文字
         */
        public Builder setRightText(@StringRes int id) {
            return setRightText(getString(id));
        }

        public Builder setRightText(CharSequence text) {
            btConfirm.setText(text);
            return this;
        }

        public Builder setListener(OnListener listener) {
            mListener = listener;
            return this;
        }

        @SingleClick
        @Override
        public void onClick(View v) {
            if (v == btConfirm) {//确定
                dismiss();
                if (mListener != null) {
                    mListener.onConfirm(getDialog());
                }
            } else if (v == btCancel) {//取消
                dismiss();
                if (mListener != null) {
                    mListener.onCancel(getDialog());
                }
            }

        }
    }

    public interface OnListener {

        /**
         * 确定
         */
        void onConfirm(BaseDialog dialog);

        /**
         * 取消
         */
        default void onCancel(BaseDialog dialog){};
    }
}